﻿using System.Drawing;
using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;
using com.fukefu.vcode;

namespace com.fukefu.info.b2b.byhand
{
    public class ShengYiDi : Information
    {
        //bool vip = false;
        VCODE code;
        //登陆
        public override bool Login(string name, string pass, string vcode)
        {
            string html = source.Get("http://users.shengyidi.com/newlogin.aspx");
            ParametersData pd = new ParametersData();
            pd.Add("__EVENTVALIDATION", Regex.Match(html, "id=\"__EVENTVALIDATION\" value=\"([^\"]+)").Groups[1].Value);
            pd.Add("__VIEWSTATE", Regex.Match(html, "id=\"__VIEWSTATE\" value=\"([^\"]+)").Groups[1].Value);
            pd.Add("txtUsersName", name);
            pd.Add("txtPassword", pass);
            pd.Add("btnSubmits.x", "44");
            pd.Add("btnSubmits.y", "13");

            html = source.Post("http://users.shengyidi.com/newlogin.aspx", pd.Read(), "http://users.shengyidi.com/newlogin.aspx");
            /*if (html.IndexOf("您目前是&nbsp;<b style=\"color:#ffd200; font-size:12px;\">普通会员</b>") == -1)
            {
                vip = true;
            }*/
            code = new VCODE(site);
            return html.IndexOf("<a href=\"/logout.aspx\">安全退出</a>") != -1;
        }
        //发布文章
        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult info = new InformationResult();
            string html = source.Get("http://users.shengyidi.com/users/publish/post_1.aspx?catid=1", "http://users.shengyidi.com/users/index.aspx");
            ParametersData pd = new ParametersData();
            pd.Add("__VIEWSTATE", Regex.Match(html, "id=\"__VIEWSTATE\" value=\"([^\"]+)").Groups[1].Value);
            pd.Add("hcktype", "0");
            pd.Add("type", "供");
            pd.Add("topCatFormKey", site.TypeId[0]);
            pd.Add("secondCatFormKey", site.TypeId[1]);
            pd.Add("pname", title);
            pd.Add("hselectid", site.TypeId[1]);
            pd.Add("x", "25");
            pd.Add("y", "8");
            html = source.Post("http://users.shengyidi.com/users/publish/post_1.aspx?catid=1", pd.Read(), "http://users.shengyidi.com/users/publish/post_1.aspx?catid=1");

            SourceFormData sfd = new SourceFormData("utf-8");
            //增加扩展词
            title = tek.Get(title);
            sfd.Write("__VIEWSTATE", Regex.Match(html, "id=\"__VIEWSTATE\" value=\"([^\"]+)\"").Groups[1].Value);
            sfd.Write("__EVENTVALIDATION", Regex.Match(html, "id=\"__EVENTVALIDATION\" value=\"([^\"]+)").Groups[1].Value);
            sfd.Write("ucategory_id", "0");
            sfd.Write("title", "供应" + title);
            sfd.Write("amount", "1");//产品数量
            sfd.Write("paging", pif.Get("产品描述"));
            sfd.Write("MiniAmount", "1");
            sfd.Write("specifications", pif.Get("产品规格"));
            sfd.Write("price", pif.Get("价格"));
            sfd.Write("i_Unit", pif.Get("单位"));
            sfd.Write("InfoPrice_Quantity", "");
            sfd.Write("InfoPrice_price", "");
            sfd.Write("InfoPrice_Quantity", "");
            sfd.Write("InfoPrice_price", "");
            sfd.Write("InfoPrice_Quantity", "");
            sfd.Write("InfoPrice_price", "");
            sfd.Write("sourceEditor", content);
            //发送文件
            sfd.WriteFile("filepic", RandomPicture.Get());
            //尾部数据
            sfd.Write("VerificationCode", GetFlushCodeImage2());
            sfd.Write("HidSubmit", "0");
            sfd.Write("hcatid", site.TypeId[1]);
            sfd.Write("hpname", title);
            //结尾
            html = source.Post("http://users.shengyidi.com/users/publish/post_2.aspx?catid=" + site.TypeId[1] + "&pname=" + MyFunction.EncodeByUtf8(title), sfd.GetContentType(), sfd.Read(), "http://users.shengyidi.com/users/publish/post_2.aspx?catid=" + site.TypeId[1] + "&pname=" + MyFunction.EncodeByUtf8(title));
            //超过20篇 停用

            //判断是否发送成功
            if (Regex.IsMatch(html, "<span>提示：信息发布成功"))
            {
                html = source.Get("http://users.shengyidi.com/users/manage/DataList.aspx");
                string id = Regex.Match(html, "供应" + title.Replace("|", "\\|") + "</a></td>\\s+<td width=\"70px\">[^>]+</td>\\s+<td width=\"13%\">[^>]+</td>\\s+<td width=\"7%\"[^>]+>\\s+<a href=\"updatetest\\.aspx\\?id=([0-9]+)&Bak=[0-9]+\">修改<").Groups[1].Value;
                if (!string.IsNullOrEmpty(id))
                {
                    info.Code = 1;
                    //info.Content = "http://www.shengyidi.com/" + (vip ? "vip" : "") + "details/" + id + "/";
                    info.Content = "http://www.shengyidi.com/show/" + id + "/";
                }
            }
            else if (Regex.IsMatch(html, "当日发布信息已经超过【[0-9]+】条限制"))
            {
                info.Code = 3;
            }
            else if (Regex.IsMatch(html, "提示：信息发布失败！"))
            {
                info.Content = "提示：信息发布失败！";
            }
            else if (Regex.IsMatch(html, "<script language='javascript'>alert\\('([^']+)'"))
            {
                info.Code = 4;
                info.Content = Regex.Match(html, "<script language='javascript'>alert\\('([^']+)'").Groups[1].Value;
            }
            return info;
        }
        //刷新验证码
        public override Image GetFlushCodeImage()
        {
            return source.GetImage("http://users.shengyidi.com/tools/VerifyImagePage.aspx", "http://users.shengyidi.com/users/publish/post_2.aspx");
        }
        public string GetFlushCodeImage2()
        {
            string s = code.Get(source.GetStream("http://users.shengyidi.com/tools/VerifyImagePage.aspx", "http://users.shengyidi.com/users/publish/post_2.aspx"));
            int _a = 0, _b = 0;
            _a = StringToInt(Regex.Match(s, "^[0-9s]+").Groups[0].Value);
            _b = StringToInt(Regex.Match(s, "[0-9s]+$").Groups[0].Value);
            if (s.IndexOf("+") != -1)
            {
                return (_a + _b).ToString();
            }
            else
            {
                return (_a - _b).ToString();
            }
        }
        private int StringToInt(string s)
        {
            int _a = 0;
            string a = Regex.Match(s, "^[0-9s]+").Groups[0].Value;
            if (a == "s")
            {
                a = "10";
            }
            a = a.Replace("s", "1");
            int.TryParse(a, out _a);
            return _a;
        }
        public override bool CheckKeyword(Title title)
        {
            if (site.BtMaxLen != 0 && title.Word.Length > site.BtMaxLen)
            {
                string[] k = Regex.Split(title.Word, "[,|\\|]");
                if (k.Length > 1)
                {
                    if (k[0].Length < site.BtMaxLen)
                    {
                        title.Word = k[0];
                        return true;
                    }
                }
                return false;
            }
            return true;
        }
    }
}
